package cn.wokoba.service.activity.service.trial.node;

import cn.wokoba.service.activity.model.entity.MarketProductEntity;
import cn.wokoba.service.activity.model.entity.TrialBalanceEntity;
import cn.wokoba.service.activity.model.valobj.GroupBuyActivityDiscountVO;
import cn.wokoba.service.activity.model.valobj.SkuVO;
import cn.wokoba.service.activity.service.trial.AbstractGroupBuyMarketSupport;
import cn.wokoba.service.activity.service.trial.factory.DefaultActivityStrategyFactory;
import cn.wokoba.service.activity.service.trial.thread.QueryGroupBuyActivityDiscountVOThreadTask;
import cn.wokoba.service.activity.service.trial.thread.QuerySkuVOFromDBThreadTask;
import cn.wokoba.types.desgin.framework.tree.IStrategyHandler;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.concurrent.*;

@Service
@Slf4j
public class TagNode extends AbstractGroupBuyMarketSupport {
    @Resource
    private MarketNode marketNode;
//    @Resource
//    private EndNode endNode;
    @Resource
    ThreadPoolExecutor threadPoolExecutor;
    @Resource
    ErrorNode errorNode;

    @Override
    protected void multiThread(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws ExecutionException, InterruptedException, TimeoutException {
        FutureTask<GroupBuyActivityDiscountVO> groupBuyActivityDiscountVOFutureTask = new FutureTask<>(
                new QueryGroupBuyActivityDiscountVOThreadTask(
                        requestParameter.getSource(), requestParameter.getChannel(), requestParameter.getGoodsId(), activityRepository));
        threadPoolExecutor.execute(groupBuyActivityDiscountVOFutureTask);

        FutureTask<SkuVO> skuVOFutureTask = new FutureTask<>(
                new QuerySkuVOFromDBThreadTask(requestParameter.getGoodsId(), activityRepository));
        threadPoolExecutor.execute(skuVOFutureTask);

        dynamicContext.setSkuVO(skuVOFutureTask.get(timeout, TimeUnit.MINUTES));
        dynamicContext.setGroupBuyActivityDiscountVO(groupBuyActivityDiscountVOFutureTask.get(timeout, TimeUnit.MINUTES));

        log.info("拼团商品查询试算服务-TagNode userId:{} 异步线程加载数据「GroupBuyActivityDiscountVO、SkuVO」完成", requestParameter.getUserId());
    }

    @Override
    protected TrialBalanceEntity doApply(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        GroupBuyActivityDiscountVO groupBuyActivityDiscountVO = dynamicContext.getGroupBuyActivityDiscountVO();
        if (groupBuyActivityDiscountVO == null) {
            return router(requestParameter, dynamicContext);
        }

        String tagId = groupBuyActivityDiscountVO.getTagId();
        boolean visible = groupBuyActivityDiscountVO.isVisible();
        boolean enable = groupBuyActivityDiscountVO.isEnable();

        if (StringUtils.isBlank(tagId)) {
            dynamicContext.setVisible(true);
            dynamicContext.setEnable(true);
            return router(requestParameter, dynamicContext);
        }
        // 当前用户是否在人群范围内；visible、enable 如果值为 ture 则表示没有配置拼团限制，那么就直接保证为 true 即可
        boolean isWithin = activityRepository.isTagCrowdRange(tagId, requestParameter.getUserId());
        dynamicContext.setVisible(visible || isWithin);
        dynamicContext.setEnable(enable || isWithin);


        return router(requestParameter, dynamicContext);
    }

    @Override
    public IStrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> get(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        // 不存在配置的拼团活动，走异常节点
        if (null == dynamicContext.getGroupBuyActivityDiscountVO() || null == dynamicContext.getSkuVO() ) {
            return errorNode;
        }

        if(!dynamicContext.isVisible()) return errorNode;

        return marketNode;
    }
}
